
package com.city.project.controller;

import com.city.project.factory.ServiceFactory;
import com.city.project.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login/login5.do")
public class RoomUpdateController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoomUpdateController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username").trim();
        String password = request.getParameter("password").trim();

        if (username == null || username.trim().length() == 0) {
            response.sendRedirect("login.jsp");
        } else if (password == null || password.trim().length() == 0) {
            response.sendRedirect("login.jsp");
        } else {
            User user = ServiceFactory.getUserService().getUserByUserName(username);
            if (user != null && password.equals(user.getPassword())) {
                HttpSession session = request.getSession();
                session.setAttribute("username", username);
                response.sendRedirect("../index/index.jsp");
            } else {
                response.sendRedirect("../hint/LoginManagersError.jsp");
            }

        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
